<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addbutton({type:1,pid:0})">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button type="text" @click="addbutton({type:1,pid:row.id})">添加</el-button>
              <el-button type="text" @click="bianjibutton(row.id)">编辑</el-button>
              <el-button type="text" @click="delbutton(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 弹窗 -->
    <el-dialog :visible="showDialog" title="弹层标题" @close="closeDialog">
      <el-form ref="addform" label-width="100px" :model="formData" :rules="rules">
        <el-form-item label="权限名称">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="getbutton">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, postPermissionList, getappList, putPermissionList, delPermissionList } from '@/api/Permission'
import transTree from '@/utils/transTree'
export default {
  data() {
    const validCode = (rule, value, callback) => {
      // 编写校验逻辑 value callback
      // 实现步骤：
      // 1. 获取到当前已经存在的所有的code + 当前的code value
      // 2. 判断所谓的重复其实就是判断 输入的code能不能在已经存在code中找到 -> includes

      // 编辑的时候: 自己和自己重复了 这时候容许
      // 解决办法: 如果是在编辑的状态下 需要在codeList中把自己去掉 filter
      // 如果当前是新增状态 codeList就是所有的  如果当前是编辑状态codeList就是去掉cacheCode之后的

      if (this.codeList.includes(value)) {
        // 重复
        callback(new Error('code已经存在不能重复'))
      } else {
        callback()
      }
    }
    return {
      list: [],
      showDialog: false,
      codeList: [],
      formData: {
        name: '',
        code: '',
        description: '',
        enVisible: '',
        type: null, // 添加的权限类型 如果是1级 type=1 如果2级 type = 2
        pid: '' // 如果点击的上面的 传固定的0 如果点击表格内 要以当前行的id为主
      },

      rules: {
        code: [
          { validator: validCode, triggers: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getPerList()
  },
  methods: {
    async  getPerList() {
      const res = await getPermissionList()
      this.list = transTree(res)
      console.log(this.list)
      this.codeList = res
    },
    addbutton(type, pid) {
      this.showDialog = true
      this.type = type
      this.pid = pid
    },
    closeDialog() {
      this.showDialog = false
      this.formData = {
        name: '',
        code: '',
        description: '',
        enVisible: '',
        type: null,
        pid: ''
      }
    },
    async getbutton() {
      if (this.formData.id) {
        // 编辑更新
        await putPermissionList(this.formData)
      } else {
        // 新增
        await postPermissionList(this.formData)
      }
      this.getPerList()
      this.$message.success('ok')
      this.showDialog = false
      // 清理表单对象
      this.formData = {
        name: '',
        code: '',
        description: '',
        enVisible: '',
        type: null,
        pid: ''
      }
    },
    async bianjibutton(id) {
      this.showDialog = true
      const res = await getappList(id)
      this.formData = res
    },
    delbutton(id) {
      this.$confirm('此操作将删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delPermissionList(id)
        this.$message.success('删除成功')
        this.getPerList()
      }).catch(() => {

      })
    }
  }
}
</script>

<style>
</style>
